Skip to content

utils_test: created function to update default kernel#4070

Merged
dzhengfy merged 1 commit intoavocado-framework:masterfrom
rh-jugraham:set_default_kernel
Apr 7, 2025
Merged

utils_test: created function to update default kernel#4070
dzhengfy merged 1 commit intoavocado-framework:masterfrom
rh-jugraham:set_default_kernel

Conversation

@rh-jugraham
Copy link
Copy Markdown
Contributor

@rh-jugraham rh-jugraham commented Mar 4, 2025

Created function to update default kernel

@rh-jugraham
Copy link
Copy Markdown
Contributor Author

@smitterl As per discussion. Review would be appreciated!

Used update_boot_option() function right above in the file as inspiration.

@rh-jugraham rh-jugraham force-pushed the set_default_kernel branch 4 times, most recently from 306951e to a47f936 Compare March 4, 2025 21:39
Comment thread virttest/utils_test/__init__.py Outdated
Comment thread virttest/utils_test/__init__.py Outdated
session.close()


def update_default_kernel(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we rename to update_vm_default_kernel()?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually we do not add new functions to init.py any more. This file is already too big.
Could you please move this function into utils_sys.py?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know there is update_boot_option() in init.py, too. Actually I prefer to move it out of the file, too, but we can do it later in another pr.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe for reuse of existing sessions in test code we could expect session as parameter instead.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@smitterl Since rebooting will result in a new session, would this imply also returning the session (either the updated one or, if no reboot, then the passed in one) for more reuse?

Also, I believe the vm needs to be a param because of, most importantly, vm.reboot() unless there is an alternative that doesn't require the vm. So the session would need to be in addition to the vm, not instead of.

@dzhengfy
Copy link
Copy Markdown
Contributor

@smitterl , I feel if update_boot_option() can be moved to another file, that will be better. So I do not suggest we add new function here. Not sure about your opinion. Welcome your feedback. Thanks

@smitterl
Copy link
Copy Markdown
Contributor

smitterl commented Mar 17, 2025

@smitterl , I feel if update_boot_option() can be moved to another file, that will be better. So I do not suggest we add new function here. Not sure about your opinion. Welcome your feedback. Thanks

I proposed to add it here because it's semantically related to the boot parameter update function but I wouldn't insist on it.

If making __init__.py smaller is the motivation then I'd be in favor of moving the two related functions into utils_sys.py but importing the old one (update boot parameters) in __init__.py. IIUC, this way existing code that uses it doesn't have to be updated but the two semantically related functions woudl live together in utils_sys.py which might also aid code exploration as opposed to keeping them in the filename __init__.py, ie.

# cat __init__.py
...
from utils_sys import update_boot_option

# cat utils_sys.py
...
def update_boot_option(...

def update_default_kernel(...

@rh-jugraham rh-jugraham force-pushed the set_default_kernel branch 3 times, most recently from e9e83b9 to cbc071b Compare March 19, 2025 22:05
@rh-jugraham
Copy link
Copy Markdown
Contributor Author

@smitterl @dzhengfy Created new PR - #4087 - for moving update_boot_option that depends on this PR (for shared imports/helper functions)

@dzhengfy
Copy link
Copy Markdown
Contributor

@smitterl @dzhengfy Created new PR - #4087 - for moving update_boot_option that depends on this PR (for shared imports/helper functions)

@rh-jugraham @smitterl I think more about this change. Moving update_boot_option() and this pr to utils_sys.py will make init.py pretty clean, but seems we also need move update_boot_option_ubuntu(), __run_cmd_and_handle_error(), check_kernel_cmdline() . Without moving them, we will have utils_test import utils_sys , and utils_sys import utils_test as well.

And if we move update_boot_option() to utils_sys.py, we have to also update tp-libvirt and tp-qemu for their reference to this function or we need still keep an encapsulating function in init.py for ease like below
in init.py

from virttest import utils_sys

def update_boot_option( ...):
   utils_sys.update_boot_option(...)

And check_kernel_cmdline() has one reference in tp-qemu (tp-libvirt does not use it) as below:
qemu/tests/boot_without_vectors.py:40: req_args = utils_test.check_kernel_cmdline(session,

Based on above analysis, it is high cost to make init.py to be pretty, at least I do not expect this happen in your PR. Maybe we use separate PR to achieve this later. So let's keep your current implementation using __init.py this time.
Please help close #4087 and thank you for submitting it anyway.

@dzhengfy
Copy link
Copy Markdown
Contributor

@rh-jugraham you may consider to make init.py in another PR later if you would like. Thanks.

Copy link
Copy Markdown
Contributor

@dzhengfy dzhengfy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@dzhengfy dzhengfy requested a review from smitterl March 20, 2025 07:09
@rh-jugraham rh-jugraham changed the title utils_test: created function to update default kernel utils_sys: created function to update default kernel Mar 20, 2025
@rh-jugraham rh-jugraham force-pushed the set_default_kernel branch 2 times, most recently from 0cc40ee to bddb26c Compare March 20, 2025 17:44
@rh-jugraham rh-jugraham changed the title utils_sys: created function to update default kernel utils_test: created function to update default kernel Mar 20, 2025
@rh-jugraham rh-jugraham requested a review from dzhengfy March 20, 2025 17:46
@rh-jugraham
Copy link
Copy Markdown
Contributor Author

@dzhengfy Moved back to utils_test as indeed, currently the two tests are too integrated with utils_test and reliant on other functions there so it makes sense for a different PR to figure out how to best seperate out the functions.

Copy link
Copy Markdown
Contributor

@dzhengfy dzhengfy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Comment thread virttest/utils_test/__init__.py Outdated
Comment thread virttest/utils_test/__init__.py Outdated
if not utils_package.package_install("grubby", session=session):
raise exceptions.TestError("Failed to install grubby package")

cmd = "grubby --default-kernel"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Grubby will use the kernel image path

# grubby --default-kernel
/boot/vmlinuz-6.13.5-200.fc41.x86_64

That makes me wonder which format you use for kernel_version, especially considering that there's some arch-specific info in that image file name.
From line 299 I assume you expect the full kernel path as there's no manipulation.
How would a user retrieve that value before calling this function? Would it make sense for this function to construct part of the full path, e.g. expecting only 6.13.5-200.fc41?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Below is some experimentation with what --set-kernel can and can't take. So some variation is accepted. However, I wouldn't be sure how to take in "6.13.5-200.fc41" as, for aarch64, there is the 64k option, so wouldn't be sure how that would work, especially if multiple kernels match the same partial string - such as differentiating between "vmlinuz-5.14.0-570.4.1.el9_6.aarch64+64k" and "vmlinuz-5.14.0-570.4.1.el9_6.aarch64" if only "5.14.0-570.4.1.el9_6" is given.

I added "Can use the entire kernel path or just be the full kernel name." to hopefully clarify that it can be either the path or just the name, but I don't know how to use just part of the kernel name without overcomplicating the function and making incorrect assumptions.

[root@ampere-mtsnow-altramax-47 /]# grubby --default-kernel
/boot/vmlinuz-5.14.0-570.4.1.el9_6.aarch64+64k

# using full output from grubby
[root@ampere-mtsnow-altramax-47 /]# grubby --set-default="kernel="/boot/vmlinuz-5.14.0-570.4.1.el9_6.aarch64""
The default is /boot/loader/entries/0eae46187a3c4734ad721259b7d1c2b5-5.14.0-570.4.1.el9_6.aarch64.conf with index 1 and kernel /boot/vmlinuz-5.14.0-570.4.1.el9_6.aarch64
[root@ampere-mtsnow-altramax-47 /]# grubby --default-kernel
/boot/vmlinuz-5.14.0-570.4.1.el9_6.aarch64

# using full path
[root@ampere-mtsnow-altramax-47 /]# grubby --set-default="/boot/vmlinuz-5.14.0-570.4.1.el9_6.aarch64+64k"
The default is /boot/loader/entries/0eae46187a3c4734ad721259b7d1c2b5-5.14.0-570.4.1.el9_6.aarch64+64k.conf with index 0 and kernel /boot/vmlinuz-5.14.0-570.4.1.el9_6.aarch64+64k
[root@ampere-mtsnow-altramax-47 /]# grubby --default-kernel
/boot/vmlinuz-5.14.0-570.4.1.el9_6.aarch64+64k

# using just full name 
[root@ampere-mtsnow-altramax-47 /]# grubby --set-default="vmlinuz-5.14.0-570.4.1.el9_6.aarch64"
The default is /boot/loader/entries/0eae46187a3c4734ad721259b7d1c2b5-5.14.0-570.4.1.el9_6.aarch64.conf with index 1 and kernel /boot/vmlinuz-5.14.0-570.4.1.el9_6.aarch64
[root@ampere-mtsnow-altramax-47 /]# grubby --default-kernel
/boot/vmlinuz-5.14.0-570.4.1.el9_6.aarch64

# using partial name (not including vmlinuz)
[root@ampere-mtsnow-altramax-47 /]# grubby --set-default="5.14.0-570.4.1.el9_6.aarch64+64k"
The param 5.14.0-570.4.1.el9_6.aarch64+64k is incorrect

Copy link
Copy Markdown
Contributor

@smitterl smitterl Mar 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To me it looks like /boot is not necessary but the full kernel image file name is.

In any case, I believe you don't have to worry about that, grubby can return all kernel paths and you can implement a function that helps select one and define it here right next to it and maybe also mention in the docstrings that it can be used to select the right kernel path:

def get_available_kernel_path(session, kernel_pattern):
    """
    Looks up all available kernels on the system and returns the first one whose image file name matches a pattern
    ...
    """
    s, output = utils_misc.cmd_status_output("grubby --info ALL", session=session ...)
    ...
    for line in output.split('\n'):
        if line.startswith("kernel=") and re.match(kernel_pattern, line):
            return line.split("=")[1]
    ...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good! I did make it so the function returns all of the kernels it found in a list in case there is some reason to find all --> perhaps might make the function more useful.

Comment thread virttest/utils_test/__init__.py Outdated
Comment thread virttest/utils_test/__init__.py Outdated
@rh-jugraham rh-jugraham force-pushed the set_default_kernel branch 2 times, most recently from eb5a6cb to 9cf857b Compare March 25, 2025 16:50
@rh-jugraham rh-jugraham requested a review from smitterl March 25, 2025 16:50
@rh-jugraham
Copy link
Copy Markdown
Contributor Author

@smitterl thanks for the suggestions! I addressed them all, except for 1 I had a question about.

@rh-jugraham rh-jugraham force-pushed the set_default_kernel branch 4 times, most recently from e2ed3e4 to ea9578e Compare March 29, 2025 00:48
… all kernel paths given some pattern

Signed-off-by: Julia Graham <jugraham@redhat.com>
Copy link
Copy Markdown
Contributor

@smitterl smitterl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM thank you very much for this!

@dzhengfy dzhengfy merged commit 567b3b3 into avocado-framework:master Apr 7, 2025
50 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants